﻿using Aker.DevTeam;
using DevExpress.Data.PivotGrid;
using DevExpress.XtraBars;
using DevExpress.XtraPivotGrid;
using DevTeam;
using Extensions;
using System;
using System.Collections.Generic;
using System.Data;

namespace Aker.Sistem
{
    public partial class BarkodKontrol : XForm
    {
        public BarkodKontrol()
        {
            InitializeComponent();
        }

        DataTable dt = new DataTable();
        private void Download()
        {
            List<Barkod> veri = AppSession.MainForm.black.GetHamVeri();

            string raf = "";
            foreach (Barkod barkod in veri)
            {
                if (barkod.Tip == Barkod.BarkodTip.Raf)
                    raf = barkod.Kod.ToString();
                dt.Rows.Add(barkod.Kod, barkod.Tip, barkod.SembolKodu, barkod.Tarih, barkod.Hata, raf, 1);
            }

            dt = Data.DM.ExecuteDataTable("GetBarkodKontrol", dt.Serialize());

            gv.Init(dt);
            gv.FormatAsDateTime("Tarih");
            gv.BestFitColumns();

            pgc.DataSource = dt;
        }

        DataTable dtx = new DataTable();
        void BarkodKontrol_BarcodeRead(int Barcode)
        {
            Barkod barkod = new Barkod(Barcode);
            barkod.Tarih = DateTime.Now;

            dt.Clear();
            string raf = "";
            if (barkod.Tip == Barkod.BarkodTip.Raf)
                raf = barkod.Kod.ToString();
            dt.Rows.Add(barkod.Kod, barkod.Tip, barkod.SembolKodu, barkod.Tarih, barkod.Hata, raf, 1);

            DataTable dtxNew = Data.DM.ExecuteDataTable("GetBarkodKontrol", dt.Serialize());
            dtx.Merge(dtxNew);
            dtx.DefaultView.Sort = "Tarih desc";
            gv.Init(dtx);
            gv.MakeColumnInvisible("Barkod", "Adet");
            gv.FormatAsDateTime("Tarih");
            gv.BestFitColumns();

            pgc.DataSource = dtx;
        }

        void pgc_CellClick(object sender, PivotCellEventArgs e)
        {
            gc.DataSource = e.CreateDrillDownDataSource();
            gv.BestFitColumns();
        }

        private void BarkodKontrol_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("Barkod", typeof(int));
            dt.Columns.Add("Tip", typeof(string));
            dt.Columns.Add("SembolKodu", typeof(int));
            dt.Columns.Add("Tarih", typeof(DateTime));
            dt.Columns.Add("Hata", typeof(string));
            dt.Columns.Add("Raf", typeof(string));
            dt.Columns.Add("Adet", typeof(int));

            pgc.Fields.Add("Barkod", PivotArea.RowArea);
            pgc.Fields["Barkod"].TotalsVisibility = PivotTotalsVisibility.None;

            PivotGridField field = new PivotGridField();
            field.Area = PivotArea.DataArea;
            field.FieldName = "Adet";
            field.SummaryType = PivotSummaryType.Count;
            pgc.Fields.Add(field);

            pgc.CellClick += pgc_CellClick;
            BarcodeRead += BarkodKontrol_BarcodeRead;
        }

        private void btnKontrol_ItemClick(object sender, ItemClickEventArgs e)
        {
            //BarkodKontrol_BarcodeRead(8002030);
            Download();
        }
    }
}